﻿using Asmodat.Abbreviate;
using Asmodat.Extensions.Collections.Generic;
using Asmodat.Extensions.Objects;
using Asmodat.IO;
using Asmodat.Types;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Asmodat_ELM
{
    public partial class Form1 : Form
    {

        //BufferedArray<string> InputHistory = new BufferedArray<string>(1024);


        public Color DeviceColor { get; set; } = Color.DarkRed;
        string buffor = "";

        public void Peacemaker_SerialPort()
        {
            var ports = _serialPort.Ports;
            var current = TCbxPort.ItemsList();
            var selected = TCbxPort.SelectedItem;
            if (ports.Count != current.Count || !current.ContainsAll(ports.ToList<object>()))
            {
                TCbxPort.SetItems(ports);

                TTSBtnConnection.Enabled = !ports.IsNullOrEmpty();

                if (selected != null && TCbxPort.Items.Contains(selected))
                    TCbxPort.SelectedItem = selected;
                else if (TCbxPort.Count > 0)
                    TCbxPort.SelectedIndex = 0;
            }

            while (!_serialPort.Data.IsAllRead)
            {
                string line;
                TickTime time;
                if (_serialPort.Data.Read(out line, out time))
                {
                    if (line.IsNullOrEmpty())
                        continue;

                    SyncBuffer += line;
                    if (SyncBuffer.Length >= 1024 * 1024) SyncBuffer = "";

                    char[] allowed = new char[] { '\t','\r','\n' };
                    string result = "";
                    foreach (char c in line)
                    {
                        if (!c.IsPrintableASCII() && !allowed.Contains(c))
                            result += string.Format("\\{0}", c.ToHex());
                        else
                            result += c;
                    }

                    buffor += result;
                }
            }


            if(!buffor.IsNullOrEmpty())
            {
                TRtbxMain.AppendText(buffor, DeviceColor, 5000);
                TRtbxMain.ScrollToEnd();
                buffor = "";
            }

            this.UpdateDictionaryControls();
        }


    }
}
/*
 //buffor += line;

                    //if (line.Last() != '\n')
                     //   continue;

                    InputHistory.Write(buffor);

                    char[] allowed = new char[] { '\t','\r','\n' };
                    string trim = (buffor.TryTrimEnd(new char[] { '\r', '\n' }));//.Escaped();
                    string result = "";
                    foreach (char c in trim)
                    {
                        if (!c.IsPrintableASCII() && !allowed.Contains(c))
                            result += string.Format("\\{0}", c.ToHex());
                        else
                            result += c;
                    }

                    TRtbxMain.AppendTextToStart(result + "\r\n", DeviceColor, 1000);
                    buffor = "";
     
     */
